package com.enterpriseresourceplanningscm.www.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
//<dependency>
//<groupId>org.slf4j</groupId>
//<artifactId>slf4j-api</artifactId>
//</dependency>
public class logConfig {
    private static final String PATHNAME="D:/log";
    private static final String  GLOBAL= "\u001b[1;34m";
    private static final String WARN = "\u001b[1;33m";
    private static final String ERROR = "\u001b[1;31m";
    private static final String INFO = "\u001b[1;32m";
    private static final String DEBUG = "\u001b[1;30m";
    private static final Date date = new Date();
    private static final SimpleDateFormat logDate = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
    private static final SimpleDateFormat fileDate = new SimpleDateFormat ("yyyy-MM-dd");
    private static final String caller = "【日志 "+"】"+logDate.format(date)+"   "+new Exception().getStackTrace()[1].getClassName() +"==>"+ GLOBAL +  new Exception().getStackTrace()[1].getMethodName()+"()";
    private static final String fileCaller = "【日志 "+"】"+logDate.format(date)+"   "+new Exception().getStackTrace()[1].getClassName() +"==>" +  new Exception().getStackTrace()[1].getMethodName()+"()\n";
    private static final Logger log = LoggerFactory.getLogger(caller);
    /**
     * 配置日志文件 内容追加
     * */
    private static void fileLogPath(String level,String info){
        File fileDir = new File(PATHNAME);
        File fileName = new File(fileDir.getPath()+"/" +"【"+level+"】"+fileDate.format(date)+".log");
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream(fileName, true)) {
            fileOutputStream.write((info+"\n").getBytes());
            fileOutputStream.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * 警告信息
     * */
    public static void warn(String msg){
        log.warn(WARN + msg);
        fileLogPath("WARN","WARN " + fileCaller + msg+"\n");
    }
    /**
     * 警告信息 附带异常
     * */
    public static void warn(String msg,Throwable throwable){
        log.warn(WARN +  msg,throwable);
        fileLogPath("WARN",fileCaller + msg +"\n" + exception(throwable));
    }
    public static void error(String msg){
        log.error(ERROR+ msg);
        fileLogPath("ERROR","ERROR " + fileCaller + msg+"\n");
    }
    public static void error(String msg,Throwable throwable){
        log.error(ERROR + msg,throwable);
        fileLogPath("ERROR",fileCaller + msg+"\n" + exception(throwable));
    }
    public static void info(String msg){
        log.info(INFO + msg);
        fileLogPath("INFO",fileCaller + msg+"\n");
    }
    public static void info(String msg,Throwable throwable){
        log.info(INFO + msg,throwable);
        fileLogPath("INFO",fileCaller + msg+"\n" +  exception(throwable));
    }
    public static void debug(String msg){
        log.debug(DEBUG + msg);
        fileLogPath("DEBUG","DEBUG " + fileCaller + msg+"\n");
    }
    public static void debug(String msg,Throwable throwable){
        log.debug(DEBUG + msg,throwable);
        fileLogPath("DEBUG",fileCaller + msg+"\n" +  exception(throwable));
    }
    private static String exception(Throwable t) {
        if(t == null)
            return null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream( 5 * 1024 * 1024);
        try{
            t.printStackTrace(new PrintStream(byteArrayOutputStream,true));
            byteArrayOutputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return byteArrayOutputStream.toString();
    }
}
